<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-09-20 12:29:57
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-09-21 15:59:44
 * @Description: 
 * 
-->
<template>
  <el-dialog  draggable overflow v-model="visible" :title="titleMap[mode]" :width="400" destroy-on-close @closed="$emit('closed')">


    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="部门" prop="department_id">
            <skAutoChoose v-model="form.department.name" :name="'financeDepartment'" :assign="form"
              :asKey="'department_id'">
            </skAutoChoose>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="期初金额" prop="amount">
            <el-input v-model="form.amount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skAutoChoose from "@/components/skAutoChoose";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose },
  props: {
    subject_id: { type: String, default: "" }
  },
  data() {
    return {
      mode: "add",

      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        department: {}
      },
      rules: {
        department_id: { required: true, message: "部门必须填写" },
        amount: { required: true, message: "金额必须填写" },
      },
      visible: false,
      isSaveing: false,

    };
  },
  mounted() {
  },
  methods: {

    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    //表单提交方法
    submit() {

      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          this.form.subject_id = this.subject_id;
          var res = await this.$API.finance.initialization.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });


    },


    //表单注入数据
    setData(data) {
      this.form = data;

    },
  },
};
</script>
<style></style>
